package mybatis.web;

import mybatis.pojo.Grade;
import mybatis.services.GradeService;
import mybatis.util.StrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author 汤舒青
 * @Version 1.0
 */

@WebServlet("/gradeUpade")
public class GradeUpdate extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        int gid = StrUtil.toInt(request.getParameter("gid"));
        out.print(gid);
        Grade grade = GradeService.ME.find(gid);
        request.setAttribute("grade", grade);
        request.getRequestDispatcher("/WEB-INF/jsp/gradeUpdate.jsp").forward(request, response);

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        int gid =StrUtil.toInt( request.getParameter("gid"));
        String gname = request.getParameter("gname");
        PrintWriter out = response.getWriter();
        System.out.println(gid);
        int update = GradeService.ME.update(new Grade(gid,gname));
        if (update > 0) {
            out.println("<script>alert('修改成功');window.location.replace('grade');</script>");
        } else {
            out.println("<script>alert('修改失败');window.location.replace('grade');</script>");
        }
    }
}
